package com.xdtech.hotel.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xdtech.core.dao.BaseDao;
import com.xdtech.core.model.BaseItem;
import com.xdtech.core.orm.Page;
import com.xdtech.core.orm.utils.BeanUtils;
import com.xdtech.hotel.dao.LinkageConfigDao;
import com.xdtech.hotel.model.DeviceOperation;
import com.xdtech.hotel.model.LinkageConfig;
import com.xdtech.hotel.service.DeviceOperationService;
import com.xdtech.hotel.service.LinkageConfigService;
import com.xdtech.hotel.util.RedisUtils;
import com.xdtech.hotel.vo.LinkageConfigItem;
import com.xdtech.web.model.Pagination;

/**
 * 
 * @author max.zheng
 * @create 2015-04-23 20:46:32
 * @since 1.0
 * @see
 */
@Service
public class LinkageConfigServiceImpl implements LinkageConfigService {
	@Autowired
	private LinkageConfigDao linkageConfigDao;
	@Autowired
	private BaseDao baseDao;
	@Autowired
	private DeviceOperationService deviceOperationService;
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-23 20:46:32
	 * @modified by
	 * @param entity
	 */
	public void save(LinkageConfig entity) {
		linkageConfigDao.save(entity);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-23 20:46:32
	 * @modified by
	 * @param entity
	 */
	public void saveAll(List<LinkageConfig> entities) {
		linkageConfigDao.saveAll(entities);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-23 20:46:32
	 * @modified by
	 * @param entity
	 */
	public void delete(LinkageConfig entity) {
		linkageConfigDao.delete(entity);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-23 20:46:32
	 * @modified by
	 * @param id
	 */
	public void delete(Long id) {
		linkageConfigDao.delete(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-23 20:46:32
	 * @modified by
	 * @param id
	 * @return
	 */
	public LinkageConfig get(Long id) {
		return linkageConfigDao.get(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-23 20:46:32
	 * @modified by
	 * @return
	 */
	public List<LinkageConfig> getAll() {
		return linkageConfigDao.getAll();
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-23 20:46:32
	 * @modified by
	 * @param pg
	 * @param values
	 * @return
	 */
	public Map<String, Object> loadPageAndCondition(Pagination pg,
			Map<String, String> values) {
		Map<String, Object> results = new HashMap<String, Object>();
		List<Object> linkageConfigs = null;
		long rows = 0;
		if (pg!=null) {
			Page<LinkageConfig> page = linkageConfigDao.findPage(pg,"from LinkageConfig order by createTime desc", values);
			linkageConfigs = BeanUtils.copyListProperties(
					LinkageConfigItem.class, page.getResult());
			rows = page.getTotalItems();
		}else {
			List<LinkageConfig> linkageConfigList = linkageConfigDao.find("from LinkageConfig order by id desc", values);
			linkageConfigs = BeanUtils.copyListProperties(
					LinkageConfigItem.class, linkageConfigList);
			rows = linkageConfigs.size();
		}
		results.put("total", rows);
		results.put("rows", linkageConfigs);
		return results;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-23 20:46:32
	 * @modified by
	 * @param item
	 * @return
	 */
	public boolean saveOrUpdateLinkageConfig(LinkageConfigItem item) {
		LinkageConfig linkageConfig = null;
		if (item.getId()==null) {
			linkageConfig = new LinkageConfig();
		}else {
			linkageConfig = linkageConfigDao.get(item.getId());
		}
		//复制前台修改的属性
		BeanUtils.copyProperties(linkageConfig, item);
		//测试redis事务
//		RedisUtils.setJsonValue();
		List<Long> selectOperations = new ArrayList<Long>();
		if (StringUtils.isNotEmpty(item.getLinkageOperations())) {
			String[] operationsIds = item.getLinkageOperations().split(",");
			for (String opId : operationsIds) {
				selectOperations.add(Long.valueOf(opId));
			}
			List<DeviceOperation> deviceOperations = deviceOperationService.loadDeviceOperations(selectOperations);
			linkageConfig.setDeviceOperations(deviceOperations);
		}
		linkageConfigDao.save(linkageConfig);
		return true;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-23 20:46:32
	 * @modified by
	 * @param linkageConfigId
	 * @return
	 */
	public LinkageConfigItem loadLinkageConfigItem(Long linkageConfigId) {
		LinkageConfig linkageConfig = linkageConfigDao.get(linkageConfigId);
		LinkageConfigItem linkageConfigItem = new LinkageConfigItem();
		BeanUtils.copyProperties(linkageConfigItem, linkageConfig);
		return linkageConfigItem;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-23 20:46:32
	 * @modified by
	 * @param id
	 * @return
	 */
	public boolean deleteLinkageConfigInfo(long id) {
		delete(id);
		return true;
	}
	
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-23 20:46:32
	 * @modified by
	 * @param newIds
	 * @return
	 */
	public boolean deleteLinkageConfigInfo(List<Long> linkageConfigIds) {
		for (Long id : linkageConfigIds) {
			delete(id);
		}
		return true;
	}
	
	public Map<String, Object> loadPageDataByConditions(Pagination pg,
			BaseItem baseItem, String queryName) {
		Map<String, Object> results = new HashMap<String, Object>();
		Page page = baseDao.findPageByNamedQuery(pg, queryName,baseItem);
		results.put("total", page.getTotalItems());
		results.put("rows", page.getResult());
		return results;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-23 20:46:32
	 * @modified by
	 * @return
	 */
	@Override
	public List<LinkageConfigItem> loadItems() {
		List<LinkageConfig> linkageConfigs = getAll();
		return LinkageConfigItem.createItems(linkageConfigs);
	}

}
